<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }

    </style>
</head>
<body onload="draw();">
</body>
<script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script>
<script src="js/utils/SceneUtils.js"></script>
<script src="js/utils/GeometryUtils.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
<script>
    var renderer;

    function initRender() {
        renderer = new THREE.WebGLRenderer({antialias: true});
        //renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); //设置背景颜色
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
    }

    var camera;

    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);
        camera.position.set(0, 200, 500);
    }

    var scene;

    function initScene() {
        scene = new THREE.Scene();
    }

    var light;

    function initLight() {
        scene.add(new THREE.AmbientLight(0x404040));

        light = new THREE.DirectionalLight(0xffffff);
        light.position.set(1, 1, 1);
        scene.add(light);
    }

    function initModel() {

        //轴辅助 （每一个轴的长度）
        object = new THREE.AxesHelper(50);
        scene.add(object);
    }

    //生成模型
    function createMesh(geom) {

        //设置当前的模型矩阵沿xy轴偏移，让图片处于显示中心
        geom.applyMatrix(new THREE.Matrix4().makeTranslation(-250, -100, 0));

        // 创建法向量纹理
        var meshMaterial = new THREE.MeshNormalMaterial({
            flatShading: THREE.FlatShading,
            transparent: true,
            opacity: 0.9
        });

        //  创建一个线框纹理
        var wireFrameMat = new THREE.MeshBasicMaterial();
        wireFrameMat.wireframe = true;

        // 创建模型
        var mesh = THREE.SceneUtils.createMultiMaterialObject(geom, [meshMaterial, wireFrameMat]);

        return mesh;
    }

    //初始化性能插件
    var stats;

    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    //用户交互插件 鼠标左键按住旋转，右键按住平移，滚轮缩放
    var controls;

    function initControls() {

        controls = new THREE.OrbitControls(camera, renderer.domElement);

        // 如果使用animate方法时，将此函数删除
        //controls.addEventListener( 'change', render );
        // 使动画循环使用时阻尼或自转 意思是否有惯性
        controls.enableDamping = true;
        //动态阻尼系数 就是鼠标拖拽旋转灵敏度
        //controls.dampingFactor = 0.25;
        //是否可以缩放
        controls.enableZoom = true;
        //是否自动旋转
        controls.autoRotate = false;
        //设置相机距离原点的最远距离
        controls.minDistance = 20;
        //设置相机距离原点的最远距离
        controls.maxDistance = 10000;
        //是否开启右键拖拽
        controls.enablePan = true;
    }

    //生成gui设置配置项
    var gui;
    var text1,text2;
    function initGui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            size: 90,
            height: 90,
            bevelThickness: 2,
            bevelSize: 0.5,
            bevelEnabled: true,
            bevelSegments: 3,
            curveSegments: 12,
            steps: 1,
            fontName: "helvetiker",
            fontWeight:"bold",
            weight: "normal",
            font:null,
            style:"italics",
            changeFont:function () {
                //创建loader进行字体加载，供后面的模型使用
                var loader = new THREE.FontLoader();
                loader.load( 'examples/fonts/gentilis_regular.typeface.json', function ( response ) {
                    gui.font = response;
                    gui.asGeom();
                } );
            },
            asGeom: function () {
                // 删除掉原来的旧模型
                scene.remove(text1);
                scene.remove(text2);
                // 重新创建模型
                var options = {
                    size: gui.size,
                    height: gui.height,
                    weight: gui.weight,
                    font: gui.font,
                    bevelThickness: gui.bevelThickness,
                    bevelSize: gui.bevelSize,
                    bevelSegments: gui.bevelSegments,
                    bevelEnabled: gui.bevelEnabled,
                    curveSegments: gui.curveSegments,
                    steps: gui.steps
                };

                text1 = createMesh(new THREE.TextGeometry("Learning", options));
                text1.position.z = -100;
                text1.position.y = 100;
                scene.add(text1);

                text2 = createMesh(new THREE.TextGeometry("Three.js", options));
                scene.add(text2);
            }
        };
        var datGui = new dat.GUI();
        //将设置属性添加到gui当中，gui.add(对象，属性，最小值，最大值）
        datGui.add(gui, 'size', 0, 200).onChange(gui.asGeom);
        datGui.add(gui, 'height', 0, 200).onChange(gui.asGeom);
        datGui.add(gui, 'fontName', ['gentilis', 'helvetiker', 'optimer']).onChange(gui.changeFont);
        datGui.add(gui, 'fontWeight', ['regular', 'bold']).onChange(gui.changeFont);
        datGui.add(gui, 'bevelThickness', 0, 10).onChange(gui.asGeom);
        datGui.add(gui, 'bevelSize', 0, 10).onChange(gui.asGeom);
        datGui.add(gui, 'bevelSegments', 0, 30).step(1).onChange(gui.asGeom);
        datGui.add(gui, 'bevelEnabled').onChange(gui.asGeom);
        datGui.add(gui, 'curveSegments', 1, 30).step(1).onChange(gui.asGeom);
        datGui.add(gui, 'steps', 1, 5).step(1).onChange(gui.asGeom);

        //调用生成一次图形
        gui.changeFont();
    }

    function render() {
        renderer.render(scene, camera);
    }

    //窗口变动触发的函数
    function onWindowResize() {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        render();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    function animate() {
        //更新控制器
        controls.update();
        render();

        //更新性能插件
        stats.update();
        requestAnimationFrame(animate);
    }

    function draw() {
        initRender();
        initScene();
        initCamera();
        initLight();
        initModel();
        initControls();
        initStats();
        initGui();

        animate();
        window.onresize = onWindowResize;
    }
</script>
</html>